package com.mychanggou.goods.controller;

import com.github.pagehelper.PageInfo;
import com.mychanggou.goods.pojo.Brand;
import com.mychanggou.goods.service.BrandService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/9/16 1:58 下午
 * @desc ：
 */
@RestController
@RequestMapping("/brand")
@CrossOrigin//跨域  ：A域名访问B域名的数据 就存在跨域；域名/端口/协议  不一致的时候就会产生跨域问题
public class BrandController {
    @Autowired
    BrandService brandService;

    /**
     * 根据分类id查询品牌信息
     * @param id
     * @return
     */
    @GetMapping("/category/{id}")
    public Result<Brand> findByCategoryID(@PathVariable("id") Integer id){
        List<Brand> byCategory = brandService.findByCategory(id);
        return new Result<Brand>(true, StatusCode.OK,"根据分类ID查询品牌成功",byCategory) ;

    }

    /***
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result<Brand> findAll(){
        List<Brand> brandList = brandService.findAll();
        return new Result<Brand>(true, StatusCode.OK,"查询成功",brandList) ;
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable Integer id){
        //根据ID查询
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true,StatusCode.OK,"查询成功",brand);
    }

    /**
     * 新增品牌数据
     * @param brand
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改品牌数据
     * @param brand
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable Integer id) {
        //设置ID
        brand.setId(id);
        //修改数据
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 多条件查询
     * @param brand
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Brand>> findList(@RequestBody(required= false) Brand brand) {
        List<Brand> list = brandService.findList(brand);
        return new Result<List<Brand>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Brand>> findPage(@PathVariable("page") int page,@PathVariable("size") int size){
        PageInfo<Brand> pageInfo = brandService.findPage(page, size);
        return new Result<PageInfo<Brand>>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 多条件查询+分页
     * @param brand
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Brand>> findPage(@RequestBody(required =false) Brand brand, @PathVariable  int page, @PathVariable int size){
        PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
        return new Result<PageInfo<Brand>>(true,StatusCode.OK,"查询成功",pageInfo);
    }




    }